//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.jeecg.p3.system.web.back;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jeecg.p3.jiugongge.util.ImageZipUtil;
import com.jeecg.p3.system.entity.JwSystemProject;
import com.jeecg.p3.system.entity.JwSystemProjectClassify;
import com.jeecg.p3.system.service.JwSystemProjectClassifyService;
import com.jeecg.p3.system.service.JwSystemProjectService;
import com.jeecg.p3.system.util.ContextHolderUtils;
import com.jeecg.p3.system.util.PropertiesUtil;
import com.jeecg.p3.system.web.LinksucaiController;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import org.apache.velocity.VelocityContext;
import org.jeecgframework.p3.core.common.utils.AjaxJson;
import org.jeecgframework.p3.core.util.SystemTools;
import org.jeecgframework.p3.core.util.WeiXinHttpUtil;
import org.jeecgframework.p3.core.util.oConvertUtils;
import org.jeecgframework.p3.core.util.plugin.ViewVelocity;
import org.jeecgframework.p3.core.utils.common.PageList;
import org.jeecgframework.p3.core.utils.common.PageQuery;
import org.jeecgframework.p3.core.utils.common.StringUtils;
import org.jeecgframework.p3.core.web.BaseController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

@Controller
@RequestMapping({"/system/back/jwSystemProject"})
public class JwSystemProjectController extends BaseController {
    public static final Logger logger = LoggerFactory.getLogger(JwSystemProjectController.class);
    static ResourceBundle bundle = ResourceBundle.getBundle("p3-web-sysconfig");
    @Autowired
    private JwSystemProjectService jwSystemProjectService;
    @Autowired
    private JwSystemProjectClassifyService jwSystemProjectClassifyService;
    private static String domain = "";
    private static String getAllProject = "";
    private static String getProjectByCode = "";
    private static String getProjectByClassifyId = "";
    private static PropertiesUtil oAuth = null;

    public JwSystemProjectController() {
    }

    @RequestMapping(
        value = {"list"},
        method = {RequestMethod.GET, RequestMethod.POST}
    )
    public void list(@ModelAttribute JwSystemProject query, HttpServletRequest request, HttpServletResponse response, @RequestParam(required = false,value = "pageNo",defaultValue = "1") int pageNo, @RequestParam(required = false,value = "pageSize",defaultValue = "10") int pageSize) throws Exception {
        PageQuery<JwSystemProject> pageQuery = new PageQuery();
        List<JwSystemProjectClassify> jwSystemProjectClassifys = this.jwSystemProjectClassifyService.queryListByAll();
        pageQuery.setPageNo(pageNo);
        pageQuery.setPageSize(pageSize);
        VelocityContext velocityContext = new VelocityContext();
        pageQuery.setQuery(query);
        velocityContext.put("jwSystemProject", query);
        String oAuthDomain = oConvertUtils.getString(LinksucaiController.getDomain());
        velocityContext.put("domain", oAuthDomain);
        logger.info("-----getPorjectPath()-------------" + getPorjectPath());
        logger.info("-----oAuthDomain-------------" + oAuthDomain);
        velocityContext.put("jwSystemProjectClassifys", jwSystemProjectClassifys);
        velocityContext.put("pageInfos", SystemTools.convertPaginatedList(this.jwSystemProjectService.queryPageList(pageQuery)));
        String viewName = "system/back/jwSystemProject-list.vm";

        String jwid =  com.jeecg.p3.jiugongge.util.ContextHolderUtils.getSession().getAttribute("jwid").toString();
        velocityContext.put("jwid",jwid);

        ViewVelocity.view(request, response, viewName, velocityContext);
    }

    @RequestMapping(
        value = {"toDetail"},
        method = {RequestMethod.GET}
    )
    public void jwSystemProjectDetail(@RequestParam(required = true,value = "id") String id, HttpServletRequest request, HttpServletResponse response) throws Exception {
        VelocityContext velocityContext = new VelocityContext();
        String viewName = "system/back/jwSystemProject-detail.vm";
        JwSystemProject jwSystemProject = this.jwSystemProjectService.queryById(id);
        velocityContext.put("jwSystemProject", jwSystemProject);
        ViewVelocity.view(request, response, viewName, velocityContext);
    }

    @RequestMapping(
        value = {"/toAdd"},
        method = {RequestMethod.GET, RequestMethod.POST}
    )
    public void toAddDialog(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws Exception {
        VelocityContext velocityContext = new VelocityContext();
        String viewName = "system/back/jwSystemProject-add.vm";
        String jwid =  com.jeecg.p3.jiugongge.util.ContextHolderUtils.getSession().getAttribute("jwid").toString();
        velocityContext.put("jwid",jwid);
        ViewVelocity.view(request, response, viewName, velocityContext);
    }

    @RequestMapping(
        value = {"/doAdd"},
        method = {RequestMethod.GET, RequestMethod.POST}
    )
    @ResponseBody
    public AjaxJson doAdd(@ModelAttribute JwSystemProject jwSystemProject) {
        AjaxJson j = new AjaxJson();

        try {
            Boolean repeat = this.jwSystemProjectService.validReat(jwSystemProject.getCode(), jwSystemProject.getId());
            if (repeat) {
                j.setSuccess(false);
                j.setMsg("项目编码重复，请重新设置");
            } else {
                jwSystemProject.setId(UUID.randomUUID().toString().replace("-", ""));
                if (jwSystemProject.getScType() != null && 2 == jwSystemProject.getScType()) {
                    jwSystemProject.setHdurl(domain + "/linksucai/link.do?linkid=" + jwSystemProject.getId());
                }

                this.jwSystemProjectService.doAdd(jwSystemProject);
                j.setMsg("保存成功");
            }
        } catch (Exception var4) {
            j.setSuccess(false);
            j.setMsg("保存失败");
        }

        return j;
    }

    @RequestMapping(
        value = {"toEdit"},
        method = {RequestMethod.GET}
    )
    public void toEdit(@RequestParam(required = true,value = "id") String id, HttpServletRequest request, HttpServletResponse response) throws Exception {
        VelocityContext velocityContext = new VelocityContext();
        JwSystemProject jwSystemProject = this.jwSystemProjectService.queryById(id);
        if (StringUtils.isNotEmpty(jwSystemProject.getProjectClassifyId())) {
            JwSystemProjectClassify jwSystemProjectClassify = this.jwSystemProjectClassifyService.queryById(jwSystemProject.getProjectClassifyId());
            velocityContext.put("jwSystemProjectClassify", jwSystemProjectClassify);
        }
        String jwid =  com.jeecg.p3.jiugongge.util.ContextHolderUtils.getSession().getAttribute("jwid").toString();
        velocityContext.put("jwid",jwid);
        velocityContext.put("jwSystemProject", jwSystemProject);
        String viewName = "system/back/jwSystemProject-edit.vm";
        ViewVelocity.view(request, response, viewName, velocityContext);
    }

    @RequestMapping(
        value = {"/doEdit"},
        method = {RequestMethod.GET, RequestMethod.POST}
    )
    @ResponseBody
    public AjaxJson doEdit(@ModelAttribute JwSystemProject jwSystemProject) {
        AjaxJson j = new AjaxJson();

        try {
            Boolean repeat = this.jwSystemProjectService.validReat(jwSystemProject.getCode(), jwSystemProject.getId());
            if (repeat) {
                j.setSuccess(false);
                j.setMsg("项目编码重复，请重新设置");
            } else {
                if (jwSystemProject.getScType() != null && 2 == jwSystemProject.getScType()) {
                    jwSystemProject.setHdurl(domain + "/linksucai/link.do?linkid=" + jwSystemProject.getId());
                }

                this.jwSystemProjectService.doEdit(jwSystemProject);
                j.setMsg("编辑成功");
            }
        } catch (Exception var4) {
            j.setSuccess(false);
            j.setMsg("编辑失败");
        }

        return j;
    }

    @RequestMapping(
        value = {"doDelete"},
        method = {RequestMethod.GET}
    )
    @ResponseBody
    public AjaxJson doDelete(@RequestParam(required = true,value = "id") String id) {
        AjaxJson j = new AjaxJson();

        try {
            this.jwSystemProjectService.doDelete(id);
            j.setMsg("删除成功");
        } catch (Exception var4) {
            j.setSuccess(false);
            j.setMsg("删除失败");
        }

        return j;
    }

    @RequestMapping(
        value = {"/doUpload"},
        method = {RequestMethod.POST}
    )
    @ResponseBody
    public AjaxJson doUpload(MultipartHttpServletRequest request, HttpServletResponse response) {
        AjaxJson j = new AjaxJson();
        try {
            MultipartFile uploadify = request.getFile("file");
            String realFilename=uploadify.getOriginalFilename();
            String fileExtension = realFilename.substring(realFilename.lastIndexOf("."));
            String filename=UUID.randomUUID().toString().replace("-", "")+fileExtension;
            String jwid =  com.jeecg.p3.jiugongge.util.ContextHolderUtils.getSession().getAttribute("jwid").toString();
            String uploadDir = com.jeecg.p3.jiugongge.util.ContextHolderUtils.getRequest().getSession().getServletContext().getRealPath("upload/img/system/"+jwid);
            File dirPath = new File(uploadDir);
            if (!dirPath.exists()) {
                dirPath.mkdirs();
            }
            String sep = System.getProperty("file.separator");
            File uploadedFile = new File(uploadDir + sep
                    + filename);
            ImageZipUtil.zipImageFile(uploadify.getInputStream(), uploadedFile, 0, 0, 0.7f);
            j.setObj(filename);
            j.setSuccess(true);
            j.setMsg("保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            j.setSuccess(false);
            j.setMsg("保存失败");
        }
        return j;
    }

    @RequestMapping(
        value = {"/getProjList"},
        method = {RequestMethod.GET, RequestMethod.POST}
    )
    @ResponseBody
    public AjaxJson recieveState(HttpServletRequest request) {
        AjaxJson j = new AjaxJson();

        try {
            PageQuery<JwSystemProject> pageQuery = new PageQuery();
            JwSystemProject query = new JwSystemProject();
            pageQuery.setPageNo(1);
            pageQuery.setPageSize(100);
            pageQuery.setQuery(query);
            PageList<JwSystemProject> projectList = this.jwSystemProjectService.queryPageList(pageQuery);
            JSONArray result = new JSONArray();
            if (projectList != null && projectList.getValues() != null && projectList.getValues().size() > 0) {
                List<JwSystemProject> pList = projectList.getValues();

                for(int i = 0; i < pList.size(); ++i) {
                    JSONObject jsonParts = new JSONObject();
                    jsonParts.put("name", ((JwSystemProject)pList.get(i)).getName());
                    jsonParts.put("bjurl", ((JwSystemProject)pList.get(i)).getBjurl());
                    jsonParts.put("img", ((JwSystemProject)pList.get(i)).getImg());
                    result.add(jsonParts);
                }
            }

            Map<String, Object> attrs = new HashMap();
            attrs.put("projectList", result);
            j.setAttributes(attrs);
            j.setSuccess(true);
        } catch (Exception var10) {
            j.setSuccess(false);
            j.setMsg("访问异常！");
            this.LOG.error("recieveState error:{}", var10.getMessage());
        }

        return j;
    }

    @RequestMapping(
        value = {"toIndex"},
        method = {RequestMethod.GET, RequestMethod.POST}
    )
    public void toIndex(HttpServletRequest request, HttpServletResponse response) throws Exception {
        VelocityContext velocityContext = new VelocityContext();
        List<JwSystemProjectClassify> jwSystemProjectClassifys = this.jwSystemProjectClassifyService.queryAllByType("1");
        velocityContext.put("jwSystemProjectClassifys", jwSystemProjectClassifys);
        velocityContext.put("jwSystemProjectRecommend", this.jwSystemProjectService.queryListByType("1"));
        velocityContext.put("jwSystemProject", this.jwSystemProjectService.queryListByType("2"));
        String jwid =  com.jeecg.p3.jiugongge.util.ContextHolderUtils.getSession().getAttribute("jwid").toString();
        velocityContext.put("jwid",jwid);
        String viewName = "system/back/newhome.vm";
        ViewVelocity.view(request, response, viewName, velocityContext);
    }

    @RequestMapping(
        value = {"getRecommendDetail"},
        method = {RequestMethod.GET, RequestMethod.POST}
    )
    @ResponseBody
    public AjaxJson getRecommendDetail(HttpServletRequest request) {
        AjaxJson j = new AjaxJson();

        try {
            j.setSuccess(true);
            String projectClassifyId = request.getParameter("id");
            List<JwSystemProjectClassify> jwSystemProjectClassifys = this.jwSystemProjectClassifyService.queryAllByBaseId(projectClassifyId);
            Map<String, Object> param = new HashMap();
            List jwSystemProjects;
            if (jwSystemProjectClassifys != null && jwSystemProjectClassifys.size() > 0) {
                jwSystemProjects = this.jwSystemProjectService.queryListByProjectClassifyId(((JwSystemProjectClassify)jwSystemProjectClassifys.get(0)).getId());
                if (jwSystemProjects != null && jwSystemProjects.size() > 0) {
                    j.setObj(jwSystemProjects);
                } else {
                    j.setObj((Object)null);
                }

                param.put("classify", jwSystemProjectClassifys);
            } else {
                jwSystemProjects = this.jwSystemProjectService.queryListByProjectClassifyId(projectClassifyId);
                if (jwSystemProjects != null && jwSystemProjects.size() > 0) {
                    j.setObj(jwSystemProjects);
                } else {
                    j.setObj((Object)null);
                }

                param.put("classify", (Object)null);
            }

            j.setAttributes(param);
        } catch (Exception var7) {
            j.setSuccess(false);
            j.setObj((Object)null);
        }

        return j;
    }

    @RequestMapping(
        value = {"toClassifyDetail"},
        method = {RequestMethod.GET, RequestMethod.POST}
    )
    @ResponseBody
    public AjaxJson toClassifyDetail(HttpServletRequest request) {
        AjaxJson j = new AjaxJson();

        try {
            String projectClassifyId = request.getParameter("id");
            List<JwSystemProject> jwSystemProjects = this.jwSystemProjectService.queryListByProjectClassifyId(projectClassifyId);
            if (jwSystemProjects != null && jwSystemProjects.size() > 0) {
                j.setObj(jwSystemProjects);
            } else {
                j.setObj((Object)null);
            }

            j.setSuccess(true);
        } catch (Exception var5) {
            j.setSuccess(false);
        }

        return j;
    }

    @RequestMapping(
        value = {"actDetail"},
        method = {RequestMethod.GET, RequestMethod.POST}
    )
    @ResponseBody
    public AjaxJson actDetail(HttpServletRequest request) {
        AjaxJson j = new AjaxJson();

        try {
            JwSystemProject jwSystemProject = this.jwSystemProjectService.queryById(request.getParameter("id"));
            String projectClassifyId = jwSystemProject.getProjectClassifyId();
            if (StringUtils.isNotEmpty(projectClassifyId)) {
                JwSystemProjectClassify jwSystemProjectClassify = this.jwSystemProjectClassifyService.queryById(projectClassifyId);
                if (jwSystemProjectClassify != null) {
                    jwSystemProject.setProjectClassifyId(jwSystemProjectClassify.getName());
                }
            }

            j.setObj(jwSystemProject);
        } catch (Exception var6) {
            j.setSuccess(false);
        }

        return j;
    }

    @RequestMapping({"/getClassifyTree"})
    @ResponseBody
    public AjaxJson getClassifyTree(HttpServletRequest request, HttpServletResponse response) {
        AjaxJson j = new AjaxJson();

        try {
            List<JwSystemProjectClassify> jwSystemProjectClassifys = this.jwSystemProjectClassifyService.queryListByAll();
            List<Map<String, String>> list = new ArrayList();
            Iterator var6 = jwSystemProjectClassifys.iterator();

            while(var6.hasNext()) {
                JwSystemProjectClassify jwSystemProjectClassify = (JwSystemProjectClassify)var6.next();
                Map<String, String> map = new HashMap();
                map.put("id", jwSystemProjectClassify.getId());
                if (StringUtils.isEmpty(jwSystemProjectClassify.getBaseId())) {
                    map.put("pId", (String) null);
                    map.put("open", "true");
                } else {
                    map.put("pId", jwSystemProjectClassify.getBaseId());
                    map.put("open", "false");
                }

                map.put("name", jwSystemProjectClassify.getName());
                list.add(map);
            }

            j.setObj(list);
            j.setSuccess(true);
        } catch (Exception var9) {
            j.setSuccess(false);
        }

        return j;
    }

    @RequestMapping(
        value = {"/getAllProject"},
        method = {RequestMethod.GET, RequestMethod.POST}
    )
    @ResponseBody
    public AjaxJson getAllProject(HttpServletRequest request, HttpServletResponse response) {
        AjaxJson j = new AjaxJson();

        try {
            Map<String, Object> attrs = new HashMap();
            String requestUrl = getAllProject;
            JSONObject jsonObj = WeiXinHttpUtil.sendPost(requestUrl);
            attrs.put("onlineProject", jsonObj);
            List<JwSystemProject> jwSystemProjectOnline = this.jwSystemProjectService.getAllProject();
            Map<String, Object> map = new HashMap();
            if (jwSystemProjectOnline.size() > 0) {
                for(int i = 0; i < jwSystemProjectOnline.size(); ++i) {
                    map.put("" + ((JwSystemProject)jwSystemProjectOnline.get(i)).getCode(), jwSystemProjectOnline.get(i));
                }
            }

            JSONObject itemJSONObj = JSONObject.parseObject(JSON.toJSONString(map));
            attrs.put("localProject", itemJSONObj);
            j.setAttributes(attrs);
        } catch (Exception var10) {
            var10.printStackTrace();
        }

        return j;
    }

    @RequestMapping(
        value = {"/getProjectByClassifyId"},
        method = {RequestMethod.GET, RequestMethod.POST}
    )
    @ResponseBody
    public AjaxJson getProjectByClassifyId(HttpServletRequest request, HttpServletResponse response) {
        AjaxJson j = new AjaxJson();

        try {
            String id = request.getParameter("id");
            String requestUrl = getProjectByClassifyId.replace("ID", id);
            JSONObject jsonObj = WeiXinHttpUtil.sendPost(requestUrl);
            List<JwSystemProject> jwSystemProjectOnline = this.jwSystemProjectService.queryListByProjectClassifyId(id);
            Map<String, Object> map = new HashMap();
            if (jwSystemProjectOnline.size() > 0) {
                for(int i = 0; i < jwSystemProjectOnline.size(); ++i) {
                    map.put("" + ((JwSystemProject)jwSystemProjectOnline.get(i)).getCode(), jwSystemProjectOnline.get(i));
                }
            }

            JSONObject itemJSONObj = JSONObject.parseObject(JSON.toJSONString(map));
            Map<String, Object> attrs = new HashMap();
            attrs.put("onlineProject", jsonObj);
            attrs.put("localProject", itemJSONObj);
            j.setAttributes(attrs);
        } catch (Exception var11) {
            var11.printStackTrace();
        }

        return j;
    }

    @RequestMapping(
        value = {"/createProject"},
        method = {RequestMethod.GET, RequestMethod.POST}
    )
    @ResponseBody
    public AjaxJson createProject(HttpServletRequest request, HttpServletResponse response) {
        AjaxJson j = new AjaxJson();

        try {
            String code = request.getParameter("code");
            String requestUrl = getProjectByCode.replace("CODE", code);
            JSONObject jsonObj = WeiXinHttpUtil.sendPost(requestUrl);
            JwSystemProject jwSystemProject = new JwSystemProject();
            jwSystemProject.setCode(code);
            jwSystemProject.setName(jsonObj.getString("name"));
            jwSystemProject.setImg(jsonObj.getString("img"));
            jwSystemProject.setHdurl(jsonObj.getString("hdurl"));
            jwSystemProject.setEntrance(jsonObj.getString("entrance"));
            jwSystemProject.setProjectClassifyId("projectClassifyId");
            this.jwSystemProjectService.doAdd(jwSystemProject);
            j.setMsg("创建完成");
            j.setObj(jsonObj);
        } catch (Exception var8) {
            var8.printStackTrace();
        }

        return j;
    }

    @RequestMapping(
        value = {"/changeUrl"},
        method = {RequestMethod.GET, RequestMethod.POST}
    )
    @ResponseBody
    public AjaxJson changeUrl(HttpServletRequest request, HttpServletResponse response) {
        AjaxJson j = new AjaxJson();

        try {
            this.LOG.info(request, "changeUrl start --------------");
            long start = System.currentTimeMillis();
            String newdomain = request.getParameter("newdomain");
            String olddomain = request.getParameter("olddomain");
            PropertiesUtil p2 = new PropertiesUtil("p3-web-sysconfig.properties");
            p2.writeProperty("oAuthDomain", newdomain);
            String classPath = getClassPath();
            String realPath = null;
            if (classPath.indexOf("/WEB-INF") != -1) {
                realPath = classPath + "/p3-web-sysconfig.properties";
            } else {
                String projectSrc = getPorjectPath();
                classPath = classPath.replace("/target", "").replace("/classes", "");
                if (classPath.indexOf("Web") == -1) {
                    classPath = projectSrc;
                }

                realPath = classPath + "/src/main/resources/p3-web-sysconfig.properties";
            }

            logger.info("------realPath------" + realPath);
            p2.writeProperties(realPath, "oAuthDomain", newdomain);
            LinksucaiController.setDomain(newdomain);
            boolean flag = this.jwSystemProjectService.changeUrl(newdomain, olddomain);
            if (flag) {
                j.setMsg(p2.readValue(realPath, "oAuthDomain"));
                j.setSuccess(true);
            } else {
                j.setSuccess(false);
            }

            this.LOG.info(request, "changeUrl time={}ms.", new Object[]{System.currentTimeMillis() - start});
        } catch (Exception var12) {
            j.setSuccess(false);
            var12.printStackTrace();
        }

        return j;
    }

    @RequestMapping(
        value = {"/changeType"},
        method = {RequestMethod.GET, RequestMethod.POST}
    )
    @ResponseBody
    public AjaxJson changeType(HttpServletRequest request, HttpServletResponse response) {
        AjaxJson j = new AjaxJson();

        try {
            logger.info("changeType start --------------");
            long start = System.currentTimeMillis();
            String realPath = getPorjectPath() + "/src/main/resources/p3-web-sysconfig.properties";
            logger.info("------realPath------" + realPath);
            String domain = oAuth.readValue(realPath, "oAuthDomain");
            boolean flag = this.jwSystemProjectService.changeType(domain);
            if (flag) {
                j.setSuccess(true);
            } else {
                j.setSuccess(false);
            }

            logger.info("changeType time={}ms.", new Object[]{System.currentTimeMillis() - start});
        } catch (Exception var9) {
            j.setSuccess(false);
            var9.printStackTrace();
        }

        return j;
    }

    private static String getPorjectPath() {
        String nowpath = System.getProperty("user.dir");
        String tempdir = nowpath.replace("bin", "webapps");
        tempdir = tempdir + "\\";
        return tempdir;
    }

    public static String getClassPath() {
        String path = JwSystemProjectController.class.getResource("/").getPath();
        return path;
    }

    static {
        oAuth = new PropertiesUtil("p3-web-sysconfig.properties");
        domain = oAuth.readProperty("oAuthDomain");
        PropertiesUtil api = new PropertiesUtil("system.properties");
        getAllProject = api.readProperty("getAllProject");
        getProjectByCode = api.readProperty("getProjectByCode");
        getProjectByClassifyId = api.readProperty("getProjectByClassifyId");
    }
}
